#include "kdcfilefactory.h"

#include <kdclib.h>

#include <QDir>
#include <QCoreApplication>

#include <KStandardDirs>
#include <KTempDir>


QString KDCFileFactory::appTempPath() const
{
	return pAppsTempPath;
}

KDCFileFactory::KDCFileFactory()
{
	KTempDir tmp;
	tmp.setAutoRemove( false );
	pAppsTempPath = tmp.name();
	if ( pAppsTempPath.endsWith( '/' ) )
		pAppsTempPath.resize( pAppsTempPath.size() - 1 );
	pAppsLogFileName = pAppsTempPath + "/kdclog.log";
}

KDCFileFactory::~KDCFileFactory()
{
	removeDirectory( appTempPath() );
}

KDCFileFactory* KDCFileFactory::getInstance()
{	
	static KDCFileFactory instance;
	return &instance;
}

QString KDCFileFactory::applicationDirPath() const
{
	QDir dir( QCoreApplication::applicationDirPath() );
	dir.cdUp();
	return dir.absolutePath();
}

QString KDCFileFactory::logFileName() const
{
	return pAppsLogFileName;
}

void KDCFileFactory::writeLogMessage( const char *aMsg )
{
	writeToFile( logFileName(), aMsg );
}

KIcon KDCFileFactory::appIcon() const
{
	return KIcon("media-optical");
}

QString KDCFileFactory::discDir( KDCDisc *aDisc ) const
{
	if ( !isNull( aDisc ) )
		return QString( appTempPath() + "/disc_%1" ).arg( aDisc->id() );
	return QString();
}

QString KDCFileFactory::discOutDir( KDCDisc *aDisc ) const
{
	if ( !isNull( aDisc ) )
		return aDisc->settings()->outputPath() + QString( "/disc_%1" ).arg( aDisc->id() );
	return QString();
}

QString KDCFileFactory::discConfigFile( KDCDisc *aDisc ) const
{
	if ( !isNull( aDisc ) )
		return QString( discDir( aDisc ) + "/discconfigfile.xml" );
	return QString();
}
/*
QString KDCFileFactory::movieTempName( KDCMovie *aMovie ) const
{
	if ( !isNull( aMovie ) )
	{
		KDCDisc *disc = getDisc( aMovie );
		return QString( discOutDir( disc ) + "/movie_%1_%2.mpg" ).arg( disc->id() ).arg( aMovie->id() );
	}
	return QString();
}

QString KDCFileFactory::movieThumbnail( KDCMovie *aMovie ) const
{
	if ( !isNull( aMovie ) )
		return movieDir( aMovie ) + "/thumbnail.png";
	return QString();
}

QString KDCFileFactory::movieSubtitleFile( KDCMovie *aMovie, int aId ) const
{
	if ( !isNull( aMovie ) )
		return QString( discDir( getDisc( aMovie ) ) + "/sub_%1_%2.txt" ).arg( aMovie->id() ).arg( aId );
	return QString();
}

QString KDCFileFactory::movieSpumuxFile( KDCMovie *aMovie ) const
{
	if ( !isNull( aMovie ) )
	{
		QString str = movieTempName( aMovie );
		str.insert( str.size() - 4, "_sub" );
		return str;
	}
	return QString();
}

QString KDCFileFactory::movieSubtitleConfigFile( KDCMovie *aMovie, int aId ) const
{
	if ( !isNull( aMovie ) )
		return QString( discDir( getDisc( aMovie ) ) + "/sub_%1_%2.xml" ).arg( aMovie->id() ).arg( aId );
	return QString();
}

QString KDCFileFactory::movieDir( KDCMovie *aMovie ) const
{
	if ( !isNull( aMovie ) )
		return QString( discDir( getDisc( aMovie ) ) + "/movie_%1" ).arg( aMovie->id() );
	return QString();
}

QString KDCFileFactory::movieChaptersDir( KDCMovie *aMovie ) const
{
	if ( !isNull( aMovie ) )
		return movieDir( aMovie ) + "/chapters";
	return QString();
}
*/